package com.example.user3.demo;

import android.app.ProgressDialog;
import android.graphics.Color;
import android.os.Bundle;
import android.text.Editable;
import android.text.InputFilter;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.text.style.ForegroundColorSpan;
import android.util.Log;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.ScaleAnimation;
import android.widget.Button;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.example.user3.demo.contract.MainContract;
import com.example.user3.demo.mvp.presenter.MainPresenter;
import com.example.user3.demo.utils.BlurTransformation;
import com.example.user3.demo.utils.CashierInputFilter;
import com.example.user3.demo.utils.MaxTextLengthFilter;
import com.example.user3.demo.wiget.CountDownImage;
import com.gyf.barlibrary.ImmersionBar;
import com.opensource.svgaplayer.SVGACallback;
import com.opensource.svgaplayer.SVGADrawable;
import com.opensource.svgaplayer.SVGAImageView;
import com.opensource.svgaplayer.SVGAParser;
import com.opensource.svgaplayer.SVGAVideoEntity;
import com.zzy.base.activity.BaseActivity;

import org.jetbrains.annotations.NotNull;

import java.text.DateFormat;
import java.text.MessageFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;


public class TestActivity extends BaseActivity<MainContract.IPresenter> implements MainContract.IView {
    TextView startText, text, tiaozheng;
    TextView resume;
    ArcLayout arcLayout;
    SVGAImageView sv;
    OvalImageView ovalImageView;

    Button button;
    //    String []lils=new String[]{
//            "3","3"
//    };
    ArrayList<String> lils;


    public static void main(String[] args) {
        System.out.println("dddddddddddd");
    }
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//
//        lils=new ArrayList<>();
//        lils.add("15");
//        String s= MessageFormat.format("看直{0}播赚{1}元", lils.toArray());
//        Log.i("zyz",s);
//        int position=s.indexOf(lils.get(0)+"");
//        int length=(lils.get(0)+"").length();
//        Log.i("zyz",length+"==");
//        SpannableString spannableString = new SpannableString(s);
//        ForegroundColorSpan colorSpan2 = new ForegroundColorSpan(Color.parseColor("#FEDD00"));
//        spannableString.setSpan(colorSpan2, position,position+1 , Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
//        button.setText(spannableString);
    }

    @Override
    protected MainContract.IPresenter setPresenter() {
        return new MainPresenter();
    }

    int progress = 0;

    @Override
    protected void initData() {
//        ImmersionBar.with(this).reset().transparentBar().init();
//        tiaozheng.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//            }
//        });
//
//        resume.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                ARouter.getInstance().build("/app/test2").navigation();
//
//            }
//        });
//
//        findViewById(R.id.set).setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//
//            }
//        });
//        findViewById(R.id.intent).setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
////                Intent intent= new Intent(TestActivity.this, MainActivity.class);
////                startActivity(intent);
//
//                ARouter.getInstance().build("/userInfo/main").navigation();
//            }
//        });
    }

    int getProgress;

    TextView tipText;
    EditText readPackMoneyEdit;
    EditText sendpackNumCountEdit;
    Button tvSubmit;
    String first = "";
    String second = "";
    boolean isOutFist;
    boolean isOutSecond;
    @Override
    protected void initViews() {
//        button=findViewById(R.id.button);
//        button.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                CountDownImage icon =findViewById(R.id.icon);
//                icon.setProgress(getProgress);
//                getProgress=getProgress+10;
//            }
//        });
//        TestFragment testFragment =TestFragment.getUserInfoFragment();
//        FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
//        fragmentTransaction.add(R.id.frame, testFragment, "").commitAllowingStateLoss();
//        resume=findViewById(R.id.resume);
//        startText=findViewById(R.id.start);
//        text=findViewById(R.id.text);
//        tiaozheng=findViewById(R.id.tiaozheng);
//        arcLayout =findViewById(R.id.ArcLayout);
//        sv=findViewById(R.id.svag);
//        ovalImageView=findViewById(R.id.oral);

        readPackMoneyEdit = findViewById(R.id.e1);
        sendpackNumCountEdit = findViewById(R.id.e2);
        tipText = findViewById(R.id.tipText);

        tvSubmit = findViewById(R.id.tvSubmit);
        tvSubmit.setText("ss"+(int)(0.05*100)+"%");
        String time = "2020-10-10T10:15:57Z";
//        date = date.replace("Z", " UTC");
        time = time.replace("Z", " UTC");//UTC是本地时间
        SimpleDateFormat format =new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss Z");
        try {
            Date  d = format.parse(time);




            Log.i("zzy",""+(d.getTime()-new Date().getTime()));
            Log.i("zzy",""+ d.compareTo(new Date()));
        } catch (ParseException e) {
            e.printStackTrace();
        }


        initInputFilter();
        initInputFilterMoney();
        readPackMoneyEdit.setFilters(new InputFilter[]{
                new MaxTextLengthFilter(TestActivity.this,100)
        });
        readPackMoneyEdit.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int start, int before, int count) {
//
//                first = charSequence.toString();
////                if(){
////
////                }
//                Log.i("zzy", "first:" + first);
//                if(!isOutFist){
//                    setLimitEditInput();
//                }
            }

            @Override
            public void afterTextChanged(Editable s) {


            }
        });
        readPackMoneyEdit.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                readPackMoneyEdit.setSelection(readPackMoneyEdit.getText().length());
            }
        });


        sendpackNumCountEdit.setFilters(filters);
        sendpackNumCountEdit.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                sendpackNumCountEdit.setSelection(sendpackNumCountEdit.getText().length());
            }
        });

        sendpackNumCountEdit.addTextChangedListener(new TextWatcher() {

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                second = s.toString();
                Log.i("zzy", "second:" + second);
//                setLimitEditInput();
                if(!isOutSecond){
                    setLimitEditInput();
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
    }


    InputFilter[] filters2;

    public void initInputFilterMoney() {
        CashierInputFilter cashierInputFilter = new CashierInputFilter("20000", "");
        cashierInputFilter.setOnlyCanSmall(false);
        cashierInputFilter.setCallback(new CashierInputFilter.OutOfRangeListener() {
            @Override
            public void callBackOutOfRange() {
                tipText.setVisibility(View.VISIBLE);
                tipText.setText("支付总金额不可超过20000元");
                isOutFist=true;
            }

            @Override
            public void callBackInOfRange() {
//                tipText.setVisibility(View.GONE);
//                setButtonEnable(true);
                isOutFist=false;
                tipText.setVisibility(View.GONE);

            }
        });
        filters2 = new InputFilter[]{cashierInputFilter};

    }

    InputFilter[] filters;

    public void initInputFilter() {
        CashierInputFilter cashierInputFilter = new CashierInputFilter("100", "");
        cashierInputFilter.setOnlyCanSmall(false);
        cashierInputFilter.setCallback(new CashierInputFilter.OutOfRangeListener() {
            @Override
            public void callBackOutOfRange() {
                tipText.setVisibility(View.VISIBLE);
                tipText.setText("一次最多发100个红包");
                isOutSecond=true;
            }

            @Override
            public void callBackInOfRange() {
//                setButtonEnable(true);
                tipText.setVisibility(View.GONE);
                isOutSecond=false;
//                setLimitEditInput()   ;

            }
        });
        filters = new InputFilter[]{cashierInputFilter};
    }

    public void setLimitEditInput() {
        String money = readPackMoneyEdit.getText().toString();
        String count = sendpackNumCountEdit.getText().toString();
        if (!TextUtils.isEmpty(money) && !TextUtils.isEmpty(count)) {
            try {
                double moneyDouble = Double.valueOf(money);
                Log.i("zzy","div:"+moneyDouble+"==="+(moneyDouble / Integer.parseInt(count)));
                if (moneyDouble / Integer.parseInt(count) > 200) {
                    tipText.setVisibility(View.VISIBLE);
                    tipText.setText("单个红包金额不可超过200元");
                    setButtonEnable(false);
                    return;
                }
                if (moneyDouble / Integer.valueOf(count) < 0.01) {
                    tipText.setVisibility(View.VISIBLE);
                    tipText.setText("单个红包不可低于0.01元");
                    setButtonEnable(false);
                    return;
                }
                tipText.setVisibility(View.GONE);
                setButtonEnable(true);

            } catch (Exception e) {
                setButtonEnable(false);
            }
        } else {
            setButtonEnable(false);
        }

    }


    public void setButtonEnable(boolean isEnable) {
        if (isEnable) {
            tvSubmit.setBackgroundColor(Color.parseColor("#00ff00"));
        } else {
            tvSubmit.setBackgroundColor(Color.parseColor("#ff0000"));
        }
    }


    @Override
    protected void initLayout(Bundle savedInstanceState) {

    }

    @Override
    protected int getLayoutId() {
        return R.layout.navi_activity;
    }

    @Override
    protected void onResume() {
        super.onResume();


    }

    @Override
    protected void onStart() {
        super.onStart();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    ProgressDialog dialog;

    @Override
    public void showLoading() {
        dialog = new ProgressDialog(this);
        dialog.show();
    }

    @Override
    public void hideLoading() {
        dialog.dismiss();
    }

    @Override
    public void success(String content) {
        text.setText(content);
    }


    private void setParse() {
        SVGAParser parser = new SVGAParser(TestActivity.this);
        parser.parse("jinb.svga", new SVGAParser.ParseCompletion() {
            @Override
            public void onComplete(@NotNull SVGAVideoEntity svgaVideoEntity) {
                SVGADrawable drawable = new SVGADrawable(svgaVideoEntity);
                sv.setImageDrawable(drawable);
                sv.startAnimation();
                sv.setCallback(new SVGACallback() {
                    @Override
                    public void onPause() {
                    }

                    @Override
                    public void onFinished() {
//                                svgaImageView.setVisibility(View.GONE);
//                                imageView.setVisibility(View.VISIBLE);
//                                incomeText("+" + CountDownTimerSupport.getInstance().getAddGoldNumber());
                    }

                    @Override
                    public void onRepeat() {

                    }

                    @Override
                    public void onStep(int i, double v) {

                    }
                });

            }

            @Override
            public void onError() {

            }
        });
//
    }

    //加载高斯模糊
    private void loadImage() {
        String url = "https://ss0.bdstatic.com/94oJfD_bAAcT8t7mm9GUKT-xh_/timg?image&quality=100&size=b4000_4000&sec=1583836353&di=6102af2dea7d565fc2bf635eee49dacb&src=http://a3.att.hudong.com/68/61/300000839764127060614318218_950.jpg";
        Glide.with(TestActivity.this)
                .load(url).apply(RequestOptions.bitmapTransform(new BlurTransformation(25, 4))).into(ovalImageView);
    }
    //放大缩小动画

    private void shakeImage() {
        ScaleAnimation animation = new ScaleAnimation(1, 1.2f, 1f, 1.2f, Animation.RELATIVE_TO_SELF, 0.5f, 1, 0.5f);
        animation.setDuration(500);
        animation.setFillAfter(true);
        animation.setRepeatCount(100);
        resume.startAnimation(animation);
        Timer ti = new Timer();
        ti.schedule(new TimerTask() {
            @Override
            public void run() {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        resume.setText("完成");
                    }
                });
            }
        }, 2000);
    }

    /**
     * String(yyyy-MM-dd HH:mm:ss) 转 Date
     *
     * @param time
     * @return
     * @throws ParseException
     */
    // String date = "2010/05/04 12:34:23";
    public static Date StringToDate(String time) throws ParseException {

        Date date = new Date();
        // 注意format的格式要与日期String的格式相匹配
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            date = dateFormat.parse(time);
            System.out.println(date.toString());
        } catch (Exception e) {
            e.printStackTrace();
        }

        return date;
    }
}
